/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-22
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.ProductPrice;
import com.rzico.basics.service.ProductPriceService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 商品价格表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-09-22
 */
@Api(description = "商品价格表接口")
@RestController
@RequestMapping("/admin/productPrice")
public class ProductPriceController extends BaseController {

    @Autowired
    private ProductPriceService productPriceService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询商品价格表
     *
     * @return
    */
    @ApiOperation("分页查询商品价格表")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "Long", paramType = "query") ,
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query")
    })
    public CommResult<ProductPrice> list(Long memberId, Long productId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (memberId!=null){
            params.put("memberId", memberId);
        }
        if (productId!=null){
            params.put("productId", productId);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProductPrice> list = productPriceService.selectList(params);
        PageResult<ProductPrice> pageResult = new PageResult<ProductPrice>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条商品价格表
     *
     * @return
    */
    @ApiOperation("查询单条商品价格表")
    @GetMapping("/find/{id}")
    public CommResult<ProductPrice> find(@PathVariable String id) {
        ProductPrice result = productPriceService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("productPrice",result);
        return CommResult.success(data);

    }

    /**
     * 保存商品价格表
     *
     * @param productPrice
     * @return
     */
    @ApiOperation("保存商品价格表")
    @PostMapping("/save")
    public CommResult<ProductPrice> save(@RequestBody ProductPrice productPrice) {
        int affectCount = productPriceService.insert(productPrice);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ProductPrice result = productPriceService.findById(productPrice.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除商品价格表
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除商品价格表", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除商品价格表,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ProductPrice> del(@PathVariable String ids) {

        productPriceService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新商品价格表
     *
     * @param productPrice
     * @return
     */
    @Log(desc = "更新商品价格表", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新商品价格表")
    @PostMapping("/update")
    public CommResult<ProductPrice> update(@RequestBody ProductPrice productPrice) {
        int affectCount = productPriceService.updateByPrimaryKeySelective(productPrice);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ProductPrice result = productPriceService.findById(productPrice.getId());
        return CommResult.success(result);
    }

}
